package fr.efrei.tradesys.struts.actions.connection;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fr.efrei.tradesys.hibernate.beans.AppUser;
import fr.efrei.tradesys.hibernate.beans.Currency;
import fr.efrei.tradesys.hibernate.beans.Owned;
import fr.efrei.tradesys.hibernate.factory.DaoFactory;
import fr.efrei.tradesys.treatments.Generators;

public class RegisterEndAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static Log				LOGGER						= LogFactory
																									.getLog(RegisterEndAction.class);
	private AppUser						u;
	private String						code;

	public String execute() throws Exception
	{
		LOGGER.info("Execution of " + this.getClass().getName());
		// Generate a login from the last name and the first name
		u.setLogin(new Generators().generateLogin(u));
		// Set the favorite currency
		Currency c = DaoFactory.getInstance().getCurrencyDao().getCurrency(code);
		u.setFavoriteCurrency(c);
		// Create the new user
		u.setEnabled(true);
		u.setAdmin(false);
		DaoFactory.getInstance().getAppUserDao().addAppUser(u);
		DaoFactory.getInstance().getOwnedDao().addOwned(new Owned(u, c, 10000));
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put("logged-in", "true");
		session.put("user", u);
		return SUCCESS;
	}

	public AppUser getU()
	{
		return u;
	}

	public void setU(AppUser u)
	{
		this.u = u;
	}

	public String getCode()
	{
		return code;
	}

	public void setCode(String code)
	{
		this.code = code;
	}

}
